package com.example.xinli;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class UpdatePasswordActivity extends AppCompatActivity {
    private EditText et_old_password;
    private EditText et_new_password;
    private EditText et_confirm_password;


    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_update_password);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        et_old_password = findViewById(R.id.et_oldPwd);
        et_new_password = findViewById(R.id.et_newPwd);
        et_confirm_password = findViewById(R.id.et_confirmPwd);
        findViewById(R.id.btn_updatePwd).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String oldPassword = et_old_password.getText().toString().trim();
        String newPassword = et_new_password.getText().toString().trim();
        String confirmPassword = et_confirm_password.getText().toString().trim();

        // 基础验证
        if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmPassword.isEmpty()) {
            Toast.makeText(UpdatePasswordActivity.this, "信息都不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 新密码和确认密码一致性检查
        if (!newPassword.equals(confirmPassword)) {
            Toast.makeText(UpdatePasswordActivity.this, "新密码和确认密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        // 检查用户名参数
        String username = getIntent().getStringExtra("username");
        if (username == null || username.isEmpty()) {
            Toast.makeText(UpdatePasswordActivity.this, "用户信息错误，请重新登录", Toast.LENGTH_SHORT).show();
            return;
        }
        // 旧密码正确性验证
        UserInfo userInfo = UserDbHelper.getInstance(UpdatePasswordActivity.this)
                .login(getIntent().getStringExtra("username"));

        if (userInfo == null) {
            Toast.makeText(UpdatePasswordActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!userInfo.getPassword().equals(oldPassword)) {
            Toast.makeText(UpdatePasswordActivity.this, "旧密码错误", Toast.LENGTH_SHORT).show();
            return;
        }

        // 检查新旧密码是否相同
        if (oldPassword.equals(newPassword)) {
            Toast.makeText(UpdatePasswordActivity.this, "新密码不能与旧密码相同", Toast.LENGTH_SHORT).show();
            return;
        }

        // 执行密码更新
        int result = UserDbHelper.getInstance(UpdatePasswordActivity.this)
                .updatePwd(getIntent().getStringExtra("username"), newPassword);

        if (result > 0) {
            Toast.makeText(UpdatePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
            setResult(1000);
            finish();
        } else {
            Toast.makeText(UpdatePasswordActivity.this, "密码修改失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
});

    }

    public void backMe(View view) {
        finish();
    }
}